﻿using MyProject.Core;
using MyProject.Entities.Address;
using MyProject.Entities.EmploymentContract;
using MyProject.Entities.User;
using MyProject.Services.ListItem;
using System.Windows.Input;
using Passport = MyProject.Entities.Passport.Passport;
using Address = MyProject.Entities.Address.Address;
using ListItem = MyProject.Entities.ListItem.ListItem;
using System.Windows.Controls;

namespace MyProject.ViewsModel.UserDialog
{
    public class UserDialogContext : NotifyPropertyChanged
    {
        private MyProject.Views.UserDialog.UserDialog _page;

        public User UserData { get; set; }
        public Passport? Passport
        {
            get
            {
                return this.UserData.passport;
            }
        }
        public EmploymentContract? Contract
        {
            get
            {
                return this.UserData?.contract;
            }
        }
        public Address? Residence
        {
            get
            {
                return this.UserData?.place_residence;
            }
        }

        public Address? Registration
        {
            get
            {
                return this.UserData?.registration;
            }
        }

        public ICommand? InputCommand { get; } = null;
        public ListItemService? Lists { get; set; } = null;

        public UserDialogContext(
            MyProject.Views.UserDialog.UserDialog page,
            User UserData
            )
        {
            this._page = page;
            this.UserData = UserData;
            this.Lists = ListItemService.getInstance();

            this.InputCommand = new RelayCommand(o => { this.InputCommandHandler(o); });
        }

        private void SaveUserData()
        {
            bool userValidation = ValidationHelper.IsFormValid(this.UserData, this._page);
            bool passportValidation = ValidationHelper.IsFormValid(this.Passport, this._page);
            bool residenceValidation = ValidationHelper.IsFormValid(this.Residence, this._page, "_1");
            bool registrationValidation = ValidationHelper.IsFormValid(this.Registration, this._page);
            bool contractValidation = ValidationHelper.IsFormValid(this.Contract, this._page);

            if (!userValidation || !passportValidation || !contractValidation || !registrationValidation || !residenceValidation)
            {
                return;
            }

            this._page.DialogResult = new Entities.MyDialogResult.MyDialogResult<User>(this.UserData, true);
            this._page.Close();
        }

        private void Cancel()
        {
            this._page.DialogResult = new Entities.MyDialogResult.MyDialogResult<User>(this.UserData, false);
            this._page.Close();
        }

        private void InputCommandHandler(object? commandType)
        {
            switch(commandType.ToString())
            {
                case "Cancel":
                    this.Cancel();
                    break;
                case "Save":
                    this.SaveUserData();
                    break;
            }
        }

        public void RegistrationCitySelectionChanged(ComboBox streetCB, System.Collections.IList e, bool setNull = false)
        {
            if (setNull)
            {
                this.Registration.street_id = null;
                this.Registration.house = "";
                this.Registration.apartment = null;
            }

            streetCB.Items.Clear();
            if (e.Count > 0)
            {
                ListItem city = (ListItem)e[0];
                if (city != null && this.Lists.Lists[(int)city.id] != null)
                {
                    foreach (ListItem street in this.Lists.Lists[(int)city.id])
                    {
                        streetCB.Items.Add(street);
                    }
                }
            }

            if (setNull)
            {
                this.Registration.NotifyPropChanged("street_id");
                this.Registration.NotifyPropChanged("house");
                this.Registration.NotifyPropChanged("apartment");
            }
        }

        public void ResidenceCitySelectionChanged(ComboBox streetCB, System.Collections.IList e, bool setNull = false)
        {
            if (setNull)
            {
                this.Residence.street_id = 0;
                this.Residence.house = "";
                this.Residence.apartment = null;
            }
            streetCB.Items.Clear();
            if (e.Count > 0)
            {
                ListItem city = (ListItem)e[0];
                if (city != null && this.Lists.Lists[(int)city.id] != null)
                {
                    foreach (ListItem street in this.Lists.Lists[(int)city.id])
                    {
                        streetCB.Items.Add(street);
                    }
                }
            }

            if (setNull)
            {
                this.Residence.NotifyPropChanged("street_id");
                this.Residence.NotifyPropChanged("house");
                this.Residence.NotifyPropChanged("apartment");
            }
        }
    }
}
